<template>
  <view>
    <base-container :contentViewStyle='style' :style="style" style="padding: 0!important;">
      <view style="background-color: #f7f7f7;">
        <base-card>
          <template #title>
            <view style="display: flex;padding: 5rpx">
              <view style="flex: 1">
                <text style="font-size: 34rpx; color: #333; font-weight: bold;">{{
                    form.mdcInstitutionShortName
                  }}
                </text>
                <uni-tag :text="form.mdcInstitutionType + '类'"
                         custom-style="background-color: #e6eeff; border-color: #e6eeff; color: #2c70fc;line-height: 1; padding: 1px 7px; margin-left: 10rpx"
                         type="primary"/>
              </view>
              <view style="font-size: 26rpx; color: #999999">
                {{ form.createTime || nowTime }}
              </view>
            </view>
          </template>
          <u-gap height="10"></u-gap>
          <view>
            <view class="cell-lx">
              <text class="hd">机构名称</text>
              <text class="bd">{{ form.mdcInstitutionName }}</text>
            </view>
            <view class='cell-lx'>
              <text class="hd">机构地址</text>
              <text class="bd">{{ form.mdcInstitutionAddress }}</text>
            </view>
            <view class='cell-lx'>
              <text class="hd">收运记录单号</text>
              <text class="bd">{{ form.serialNo }}</text>
            </view>
          </view>
        </base-card>
        <uni-card v-if="form.status == 'abnormal'" :border="false" :isShadow="false" is-full title="异常信息">
          <view>
            <view class="cell-lx">
              <text class="hd">车牌号</text>
              <text class="bd">{{ form.licensePlate }}</text>
            </view>
            <view class="cell-lx">
              <text class="hd">驾驶员</text>
              <text class="bd">{{ form.driverName }}</text>
            </view>
            <view class="cell-lx">
              <text class="hd">装卸员</text>
              <text class="bd">{{ form.loUnPerson }}</text>
            </view>
            <view class="cell-lx">
              <text class="hd">收运人签字</text>
              <text class="bd">{{ form.collectionTransportPerson }}</text>
            </view>
            <view class="cell-lx">
              <text class="hd">异常时间</text>
              <text class="bd">{{ form.abnormalTime }}</text>
            </view>
            <view class="cell-lx">
              <text class="hd">异常类型</text>
              <text class="bd">{{ form.abnormalType }}</text>
            </view>
            <view class="cell-lx">
              <text class="hd">异常原因</text>
              <text class="bd">{{ form.abnormalReason }}</text>
            </view>
            <view>
              <view class="cell-lx">
                <text class="hd">异常图片</text>
              </view>
              <base-images v-model="form.abnormalPic" :disabled="true"/>
            </view>
          </view>
        </uni-card>
        <template v-else>
          <uni-card :border="false" :isShadow="false" is-full title="感染性医疗废物(KG)(841-001-01)">
            <view>
              <view class="cell-lx">
                <text class="hd">桶数</text>
                <text class="bd">{{ form.infectionBarrelsCount }}桶</text>
              </view>
              <view class='cell-lx'>
                <text class="hd">毛重</text>
                <text class="bd">{{ form.infectionGrossWeight }}kg</text>
              </view>
              <view class='cell-lx'>
                <text class="hd">净重</text>
                <text class="bd">{{ form.infectionNetWeight }}kg</text>
              </view>
            </view>
          </uni-card>
          <uni-card :border="false" :isShadow="false" is-full title="损伤性医疗废物(KG)(841-002-01)">
            <view>
              <view class="cell-lx">
                <text class="hd">桶数</text>
                <text class="bd">{{ form.sharpsBarrelsCount }}桶</text>
              </view>
              <view class='cell-lx'>
                <text class="hd">毛重</text>
                <text class="bd">{{ form.sharpsGrossWeight }}kg</text>
              </view>
              <view class='cell-lx'>
                <text class="hd">净重</text>
                <text class="bd">{{ form.sharpsNetWeight }}kg</text>
              </view>
            </view>
          </uni-card>
          <uni-card :border="false" :isShadow="false" is-full title="合计">
            <view>
              <view class="cell-lx">
                <text class="hd">合计（净重）</text>
                <view class="bd flex-ac">{{ form.totalWasteNetWeight }}kg
                  <text style="font-size: 24rpx;color: #999999;padding-left: 10rpx;">(每个空桶重4.3kg)</text>
                </view>
              </view>
              <view class='cell-lx'>
                <text class="hd">交接人</text>
                <text class="bd">{{ form.transferPerson }}</text>
              </view>
            </view>
          </uni-card>
          <uni-card :border="false" :isShadow="false" is-full title="收运信息">
            <view>
              <view class="cell-lx">
                <text class="hd">接收时间</text>
                <text class="bd">{{ form.receivingTime }}</text>
              </view>
              <view class="cell-lx">
                <text class="hd">是否属实</text>
                <text class="bd">{{ form.truthfulness == 'Y' ? '是' : '否' }}</text>
              </view>
              <view class="cell-lx">
                <text class="hd">复核重量</text>
                <text class="bd">{{ form.revisedWeight }}</text>
              </view>
              <view class="cell-lx">
                <text class="hd">收运人签字</text>
                <text class="bd">{{ form.collectionTransportPerson }}</text>
              </view>
              <view class="cell-lx">
                <text class="hd">车牌号</text>
                <text class="bd">{{ form.licensePlate }}</text>
              </view>
              <view class="cell-lx">
                <text class="hd">驾驶员</text>
                <text class="bd">{{ form.driverName }}</text>
              </view>
              <view class="cell-lx">
                <text class="hd">装卸员</text>
                <text class="bd">{{ form.loUnPerson }}</text>
              </view>
              <view class="cell-lx">
                <text class="hd">异常备注</text>
                <text class="bd">{{ form.abnormalRemark }}</text>
              </view>
              <view>
                <view class="cell-lx">
                  <text class="hd">收运记录单照片</text>
                </view>
                <base-images v-model="form.collectionTransportPlanPic" :disabled="true"/>
              </view>
              <view>
                <view class="cell-lx">
                  <text class="hd">电子签章</text>
                </view>
                <base-images v-model="form.signatures" :disabled="true"/>
              </view>
            </view>
          </uni-card>
        </template>
      </view>
    </base-container>
  </view>
</template>

<script>
import {TaskPlanApi as Api} from "@/comms/api/taskPlanApi";
import {DateUtil, getUserName, Msg, toBack} from "@/utils/BaseUtils";

export default {
  data() {
    return {
      params: {},
      form: {
        receivingTime: "",
      },
      location: {},
      nowTime: DateUtil.getCurrDateTime(),
      driverList: [],
      loUnPersonnelList: [],
      vehicleList: [],
      style: {
        'background-color': '#f7f7f7'
      }
    };
  },
  onLoad(params) {
    this.params = params;
  },
  mounted() {
    this.loadDate(this.params)
  },
  methods: {
    async loadDate({taskPlanId, mdcInstitutionId}) {
      Msg.loading(async close => {
        await Promise.all([
          Api.taskPlanVehicleList({taskPlanId}).then(({data}) => {
            this.vehicleList = (data || []).map(val => ({value: val.vehicleId, text: val.licensePlate}))
          }),
          Api.taskPlanDriverList({taskPlanId}).then(({data}) => {
            this.driverList = (data || []).map(val => ({value: val.ctPersonId, text: val.personName}))
          }),
          Api.taskLoUnPersonnelList({taskPlanId}).then(({data}) => {
            this.loUnPersonnelList = (data || []).map(val => ({value: val.ctPersonId, text: val.personName}))
          }),
        ]);
        if (taskPlanId && mdcInstitutionId) {
          await Api.medicalWasteTransferDetail({taskPlanId, mdcInstitutionId}).then(({data}) => this.form = {...data})
        }
        this.form.collectionTransportPerson = getUserName();
        this.form.receivingTime = this.form.receivingTime || DateUtil.getCurrDateTime()

        close?.();
      })
    },

    submit() {
      Msg.loading(close => {
        const {form = {}, location = {}} = this;
        // 计算是否在电子围栏内
        // if (form.electronicFence > 0) {
        //   const distance = GeoUtils.calculateDistance({
        //         lat: form.mdcInstitutionLatitude,
        //         lon: form.mdcInstitutionLongitude
        //       },
        //       {lat: location.latitude, lon: location.longitude});
        //   if (distance > (form.enclosure * 1.3)) {
        //     close?.();
        //     return Msg.error("您不在围栏区间内")
        //   }
        // }

        this.$refs.form.validate().then(async (valid) => {
          Msg.confirm("确认收运吗？").then(() => {
            Api.receiveAdd(this.convert())
                .then(res => {
                  Msg.success("保存成功")
                  uni.$emit('plannedRouterReload')
                  uni.$emit('taskIndexReload')
                  setTimeout(() => toBack?.(), 1000)
                }).finally(() => close?.())
          }).catch(() => close?.())
        }).catch(() => close?.())
      })
    },
    revisedWeightChange(form = {}) {
      if (form.truthfulness == 'Y') {
        form.revisedWeight = form.totalWasteNetWeight
      }
    },
    convert() {
      const {form = {}, vehicleList = [], itemList = []} = this;
      return {

        "id": form.id,
        "taskPlanId": form.taskPlanId,
        "mdcInstitutionId": form.mdcInstitutionId,

        "infectionBarrelsCount": form.infectionBarrelsCount,
        "infectionGrossWeight": form.infectionGrossWeight,
        "infectionNetWeight": form.infectionNetWeight,

        "sharpsBarrelsCount": form.sharpsBarrelsCount,
        "sharpsGrossWeight": form.sharpsGrossWeight,
        "sharpsNetWeight": form.sharpsNetWeight,

        "totalWasteNetWeight": form.totalWasteNetWeight,
        "transferPerson": form.transferPerson,

        "receivingTime": form.receivingTime,
        "truthfulness": form.truthfulness,
        "revisedWeight": form.revisedWeight,
        "collectionTransportPerson": form.collectionTransportPerson,
        "vehicleId": form.vehicleId,
        "driverId": form.driverId,
        "loUnPersonId": form.loUnPersonId,
        "abnormalRemark": form.abnormalRemark,
        "collectionTransportPlanPic": form.collectionTransportPlanPic,

        longitude: this.location.longitude,
        latitude: this.location.latitude,
      }
    }
  }
}
</script>
<style>
page {
  background: #f7f7f7 !important;
}
</style>
<style lang="scss" scoped>
.uni-card {
  margin-bottom: 15rpx;
}

v-deep .uni-forms-item__label {
  font-size: 40rpx !important;
}

v-deep .uni-card--full {
  margin-top: 20rpx !important;
}

.cell-lx {
  .hd {
    width: 200rpx !important;
  }
}

</style>
